package se.ytterman.jserial.framework.device;

import se.ytterman.jserial.framework.messaging.Event;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.util.regex.*;

import se.ytterman.jserial.util.*;

public class Request extends Event {

    public Request(String requestId, 
                   String requestName, 
                   String formatString) {
	this.requestId = requestId;
        this.requestName = requestName;
        this.formatString = formatString;
    }

    public String getRequestId() {
        return this.requestId;        
    }

    public String getRequestFormat() {
        // Create object array
        Collection parameterValues = this.eventParameters.values();
        Object[] parameterObjects = parameterValues.toArray();
        
        String requestFormat = String.format(this.formatString, parameterObjects);
        
        return requestFormat;
    }
    
    public String getRequestString() {
        // Format string
        String requestString = this.getRequestFormat();
        requestString = requestString.replace("\\r", "\r");
        requestString = requestString.replace("\\n", "\n");
        
	return requestString;
    }   
    
    public String toString() {
        String result = this.requestName + " (" + this.requestId + ") "  +
                        "Parameters: " + this.eventParameters.toString() +
                        " -> " + this.getRequestFormat();
                
        return result;
    }
    
    public byte[] toByteArray() {
        String requestString = this.getRequestString();
               
        return requestString.getBytes();
    }
    
    private String requestId;
    private String requestName;
    private String formatString;
}
